﻿Imports WiremetModel.Entities.SupplyOrders
Imports WiremetModel.Entities.OEM
Imports InputValidation
Imports InputValidation.GeneralRules
Imports InputValidation.GeneralRules.NumericInputRules
Imports System.ComponentModel

Public Class WireCertificatePackagingInputDialog

    Private _packaging As WireCertificatePackaging
    Public Property Packaging() As WireCertificatePackaging
        Get
            Return _packaging
        End Get
        Set(ByVal value As WireCertificatePackaging)
            _packaging = value
        End Set
    End Property

    Private _inputValidator As InputValidator


    Public Sub New()
        InitializeComponent()
        ClearErrorLabels()
        _packaging = New WireCertificatePackaging()
        _inputValidator = New InputValidator()
    End Sub

    Public Sub New(ByVal packaging As WireCertificatePackaging)
        InitializeComponent()
        ClearErrorLabels()
        _inputValidator = New InputValidator()
        _packaging = packaging

        txt_Count.Text = _packaging.Count.ToString()
        txt_Name.Text = _packaging.Name
        txt_Weight.Text = _packaging.Weight.ToString()

    End Sub


    Private Sub ClearErrorLabels()
        lbl_CountError.Text = String.Empty
        lbl_NameError.Text = String.Empty
        lbl_WeightError.Text = String.Empty
    End Sub


#Region "Input validation"

    Private Sub txt_Name_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Name.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Name, e, lbl_NameError, notNullable)
    End Sub

    Private Sub txt_Name_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Name.Validated
        lbl_NameError.Text = String.Empty
    End Sub


    Private Sub txt_Weight_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Weight.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim notNegative As New NumericNotNegativeInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Weight, e, lbl_WeightError, notNullable.And(numeric).And(notNegative))
    End Sub

    Private Sub txt_Weight_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Weight.Validated
        lbl_WeightError.Text = String.Empty
    End Sub

    Private Sub txt_Count_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Count.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim positive As New NumericPositiveInputValidatingSpecification()
        Dim int As New IntegerInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Count, e, lbl_CountError, notNullable.And(positive).And(int))
    End Sub

    Private Sub txt_Count_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Count.Validated
        lbl_CountError.Text = String.Empty
    End Sub

#End Region

    

    Private Sub btn_Accept_Click(sender As System.Object, e As System.EventArgs) Handles btn_Accept.Click
        Try
            If Not ValidateChildren() Then
                Throw New EntityDataInputException("Podane dane są nieprawidłowe." & vbNewLine &
                                                   "Popraw je i spróbuj ponownie.")
            End If

            Dim confirmationDialogResult As DialogResult = MessageBox.Show("Czy jesteś pewień?", "Uwaga!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If confirmationDialogResult = Windows.Forms.DialogResult.Yes Then

                With _packaging
                    .Count = Int32.Parse(txt_Count.Text.Trim())
                    .Name = txt_Name.Text.Trim()
                    .Weight = Decimal.Parse(txt_Weight.Text.Trim())
                End With
                DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As EntityDataInputException
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class